package com.zw.april14thh.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zw.april14thh.entity.Business;
import com.zw.april14thh.entity.QueueNumber;
import com.zw.april14thh.service.BusinessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
public class QueueIdconfig {
    @Autowired
    BusinessService businessService;
    @Autowired
    QueueNumber queueNumber;

    @PostConstruct     //启动立即执行
    @Scheduled(cron = "0 0 0-12 * * ? ")
    public void updateQueueId(){

        LocalDate today = LocalDate.now();

        Business one = businessService.getOne(new QueryWrapper<Business>()
                        .orderByDesc("start_time")
                .last("limit 1")
        );
        LocalDate startTime = one.getStartTime().toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
        if (today.getYear()==startTime.getYear()&&today.getMonthValue()==startTime.getMonthValue()&&today.getDayOfMonth()==startTime.getDayOfMonth()){
            System.out.println("--------------------------------日期一致不需要更新");
        }else {
            System.out.println("--------------------------------日期不一致需要更新");
            queueNumber.resetting();
        }

    }
}
